package net.vinote.smart.platform.service.handler;

import java.util.Date;

import net.vinote.smart.platform.comm.DateFunction;
import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.exception.PlatformException;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.security.PlatformSecurity;
import net.vinote.smart.platform.service.GeneralTools;
import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.dw.handler.HandlerPerimissionAnnotation;
import net.vinote.smart.platform.service.dw.handler.VirtualCommonHandler;
import net.vinote.smart.platform.service.enumerate.CodeCatalogEnum;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;
import net.vinote.smart.platform.service.util.Permission.PermissionRelation;

import org.json.JSONObject;

/**
 * 系统许可证注册Handler
 * 
 * @author Seer
 * @version LicenseRegistHandler.java, v 0.1 2015年3月11日 上午10:37:53 Seer Exp.
 */
@HandlerPerimissionAnnotation(
		virtual = @Permission(
				value = PermissionEnum.SMART_DISABLE,
				PermissionRelation = PermissionRelation.NOT))
public class LicenseRegistVirtualHandler extends VirtualCommonHandler {

	@Override
	public void initDisplayForVirtual(BizObject bo) {
		String registDate = DateFunction.getDate(new Date(), "yyyyMMddHHmmss");
		String machineCode = GeneralTools.encrypt(
				PlatformSecurity.getHardwareAddress(),
				GeneralTools.getAesKey(registDate));
		bo.setAttribute("machineCode", machineCode);
		bo.setAttribute("registDate", registDate);
	}

	@Override
	public void doVirtual(BizObject bo) throws Exception {
		String license = bo.getAttribute("license");
		String address = PlatformSecurity.getHardwareAddress();
		JSONObject json = new JSONObject(PlatformSecurity.decrypt(license));
		String registUser = json.getString("user");
		String expireDate = json.getString("expire");
		String token = json.getString("token");
		// 校验信息合法性
		String regCode = PlatformSecurity.generateRegistrationCode(registUser,
				expireDate);
		Date now = new Date();
		if (!token.equals(regCode)) {
			throw new PlatformException("注册码无效");
		}
		if (DateFunction.getDate(expireDate, "yyyy-MM-dd").getTime() <= now
				.getTime()) {
			throw new PlatformException("注册码过期");
		}
		DBManager bm = DBManagerFactory.getInstance("code_library");
		BizObject licenseBo = bm
				.createQuery(SRE.getTable("code_library").getFieldsName(),
						"codeNo=? and itemNo=?")
				.setParamVal(CodeCatalogEnum.RegistrationCode.getCode(),
						address).getSingleResult();
		if (licenseBo == null) {
			licenseBo = new BizObject();
			licenseBo.setAttribute("codeNo",
					CodeCatalogEnum.RegistrationCode.getCode());// 代码类目
			licenseBo.setAttribute("ItemNo", address);// 权限值
			licenseBo.setAttribute("ItemName", address + " License");// 权限名称
			licenseBo.setAttribute("IsInUse", PlatformConst.YES);// 描述列的注释
			licenseBo.setAttribute("SORTNO", "0000");
			licenseBo.setAttribute("attr01", json.toString());// 设置注册码
			bm.saveBizObject(licenseBo);
		} else {
			licenseBo.setAttribute("IsInUse", PlatformConst.YES);
			licenseBo.setAttribute("attr01", json.toString());// 设置注册码
			bm.updateBizObject(licenseBo);
		}
		SRE.getInstance().setLicense(true);
	}

}
